<template>
    <el-form style="width: 60%" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="ID" prop="id">
            <el-input v-model="ruleForm.id"></el-input>
        </el-form-item>
        <el-form-item label="部门名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="部门排序(值越小越靠前)" prop="name">
            <el-input v-model="ruleForm.px" ></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
            <el-button @click="back()">返回</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    export default {
        name: "AddTCollege",
        data(){
            return{
                ruleForm:{
                    name: '',
                    id: '',
                    px: '0'
                },
                rules:{
                    name:[
                        {
                            required: true,
                            message: '部门信息不能为空',
                            trigger: 'blur'
                        }
                    ],
                    id:[
                        {
                            required: true,
                            message: 'id不能为空',
                            trigger: 'blur'
                        }
                    ],
                    px:[
                        {
                            trigger: 'blur'
                        }
                    ]
                }
            }
        },
        methods:{
            back(){
                this.$router.push("/QueryTCollege")
            },
            submitForm(formName){
                const _this=this;
                this.$refs[formName].validate((valid)=>{
                    if(valid){
                        axios.post('http://localhost:8181/teacher/addCollege',this.ruleForm).then(function (resp) {
                            if (resp.data==="添加成功"){
                                _this.$alert("部门 "+_this.ruleForm.name+" 添加成功","消息",{
                                    confirmButtonText:'知道了',
                                    callback: action => {
                                        _this.$router.push("/QueryTCollege")
                                    }
                                });
                            }
                            else{
                                _this.$alert("部门 "+_this.ruleForm.name+" 添加失败,请检查是否重复添加","消息",{
                                   confirmButtonText:"知道了",
                                   callback:action => {
                                       _this.$router.push("/QueryTCollege")
                                   }
                                });
                            }
                        })
                    }
                    else{
                        return false;
                    }
                    }
                );
            },
            resetForm(formName){
                this.$refs[formName].resetField();
            }
        }
    }
</script>

<style scoped>

</style>
